Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

226
Visualizações
chrome.scripting.executeScript MV3 | How to set local scope property value from the callback scope

Documentation

Consider this code:

// background.js
function Action(tab) {
    let title;
    function domTitle() {
        return document.title;
    }

    chrome.scripting.executeScript({target: {tabId: tab.id}, func: domTitle}, injectionResults => { 
        for (const frameResult of injectionResults) {
            this.title = frameResult.result;
            console.log(this);       // line 10
            console.log(this.title); // line 11
        }
    });
}

chrome.contextMenus.onClicked.addListener(((info, tab) => {
    const action = new Action(tab);
    console.log(action);        // line 29
    console.log(action.title);  // line 30
}));

Console output: Console screenshot

Why line 30 ( the title property title of Action ) is undefined? The property has clearly been set.

How to fix it? So line 30 will hold the value that was set by chrome.scripting.executeScript.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Pretty much all the chrome.* functions from the new v3 manifest api return promises.

Here is my solution:

// background.js
const contextMenu = () => {
    chrome.contextMenus.create({
        "id": "myIdContextMenu",
        "title": "My Context Menu Title",
        "documentUrlPatterns": [
            "http://*/*",
            "https://*/*"
        ]
    });
};

async function getValues(tab) {
    function domTitle() {
        return document.title;
    }

    const dom = chrome.scripting.executeScript({target: {tabId: tab.id}, func: domTitle})
        .then(result => {return {name: "cookies", values: result}});
    const cookies = chrome.cookies.getAll({domain: "google.com"})
        .then(result => {return {name: "dom", values: result}});

    return await Promise.all([cookies, dom]);
}

chrome.contextMenus.onClicked.addListener((info, tab) => {
    getValues(tab).then(values => {
        console.log(values);

        // Rest of my logic here.
    })
    .catch(error => {
      console.error(error.message)
    });
});

chrome.runtime.onStartup.addListener(contextMenu);
chrome.runtime.onInstalled.addListener(contextMenu);
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda